//
//  Hero.m
//  TowerDefence
//
//  Created by k3 on 6/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Hero.h"
#import "PlayScreen.h"
@implementation Hero

-(id)initHeroWithGameLayer:(GameLayer*)_gameLayer{
    self=[super initSoldierWith:_gameLayer];
    HP=10000;
    armor=50;
    speed=1.0f;
    damage=20;
    attackRange=30.0f;
    return self;
}

-(void)update:(ccTime)_timeUpdate{
    [super update:_timeUpdate];
    //NSLog(@"Standing Position x=%f y=%f",spriteBody.position.x,spriteBody.position.y);
}

-(void)animationBattle {
    inBattle=YES;
    [spriteBody stopAllActions];
    [super animationBattle];
    CCAnimation *animation = [self loadAnimationFromCacheByName:@"Hero_Attack" withCount:6 delay:0.1];
    
    CCCallFunc *func=[CCCallFunc actionWithTarget:self selector:@selector(battle)];
    [spriteBody runAction:[CCRepeatForever actionWithAction:[CCSequence actions:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:YES], func,[CCDelayTime actionWithDuration:0.5], nil]]];
}

-(void)animationMoveLeft{
    NSLog(@"Moving");
    [super animationMoveLeft];
    CCAnimation *animation = [self loadAnimationFromCacheByName:@"Hero_Move" withCount:5 delay:0.1];
    [spriteBody runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:YES]]];
}

-(void)animationDie{
    [super animationDie];
    CCAnimation *animation = [self loadAnimationFromCacheByName:@"Hero_Die" withCount:6 delay:0.1];
    CCCallFunc *dieCallBack = [CCCallFunc actionWithTarget:self selector:@selector(dieCallBack)];
    [spriteBody runAction:[CCSequence actions:[CCAnimate actionWithAnimation:animation], [CCDelayTime actionWithDuration:0.5f], dieCallBack, nil]];
}

-(void)summonHero{
    CCSprite *summonCircle=[CCSprite spriteWithSpriteFrameName:@"Circle_01.png"];
    summonCircle.position=standingPosition;
    summonCircle.scale=0;
    [((PlayScreen*)handler).mapBackgroundSprite addChild:summonCircle];
    CCAnimation *animation = [self loadAnimationFromCacheByName:@"Circle" withCount:38 delay:0.05];
    
    CCCallFunc *func=[CCCallFunc actionWithTarget:self selector:@selector(heroAppear)];
    [summonCircle runAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.8 scale:1],[CCAnimate actionWithAnimation:animation restoreOriginalFrame:YES], func,[CCScaleTo actionWithDuration:0.8 scale:0], nil]];
}

-(void)heroAppear{
    spriteBody=[[CCSprite alloc] initWithSpriteFrameName:@"Hero_Move_01.png"];
    [self drawBloodMeterWithLength:spriteBody.contentSize.width/2 + 10 width:3 position:ccp(spriteBody.contentSize.width/2, spriteBody.contentSize.height)];
    spriteBody.scale=0;
    spriteBody.position=ccp(standingPosition.x+30,standingPosition.y);
    [((PlayScreen*)handler).mapBackgroundSprite addChild:spriteBody];
    [handler addGameObject:self];
    [spriteBody runAction:[CCScaleTo actionWithDuration:0.3 scale:1]];
}

-(void)dealloc {
    NSLog(@"Goblin dealoc");
    [self removeAnimationInFrameCache:@"Hero_Attack"];
    [self removeAnimationInFrameCache:@"Hero_Move"];
    [self removeAnimationInFrameCache:@"Hero_Die"];
    [super dealloc];
}


@end
